<template>
	<div class="layout-padding">
		
		<div class="account_title_wrapper">
			<el-card shadow="never">
				<div class="account_amount_text">净资产</div>
				<div class="account_amount_value">{{ stateData.totalAssets }}</div>
			</el-card>
		</div>
		
		
		<div class="layout-padding-auto layout-padding-view">
			<!--			当前页面无需查询，因为个人资产固定就几个最多两三页即可，不存在大量数据-->
			<el-row>
				<div class="mb8" style="width: 100%">
					<!--          <el-button plain :disabled="multiple" icon="Delete" type="primary"
											v-auth="'account_accAccounts_del'" @click="handleDelete(selectObjs)">
											删 除
										</el-button>-->
					<right-toolbar v-model:showSearch="showSearch" :export="'account_accAccounts_export'"
												 @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
												 @queryTable="getDataList">
						<!-- 导出 -->
						<el-tooltip class="item" effect="dark" content="添加资产" placement="top">
							<el-button v-auth="'account_accAccounts_add'" circle icon="Plus" @click="formDialogRef.openDialog()" />
						</el-tooltip>
					</right-toolbar>
				</div>
			</el-row>
			<el-table :data="state.dataList" v-loading="state.loading" border
								:cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
								@selection-change="selectionChangHandle"
								@sort-change="sortChangeHandle">
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="#" width="40" />
				<el-table-column prop="accountName" label="账户名称"  show-overflow-tooltip>
					<template #default="scope">
						<a @click="openDetail(scope.row.id, scope.row.accountName)">{{scope.row.accountName}}</a>
					</template>
				</el-table-column>
				<el-table-column prop="accountType" label="账户类型"  show-overflow-tooltip>
					<template #default="scope">
						<el-tag
							:type="'primary'"
							disable-transitions
						>{{ scope.row.accountTypeName }}</el-tag
						>
					</template>
				</el-table-column>
				<el-table-column prop="balance" label="账户余额"  show-overflow-tooltip>
					<template #default="scope">
						<span style="color: #f56c6c;font-style: italic">{{ typeof scope.row.balance === 'number' ? scope.row.balance.toFixed(2) : scope.row.balance }}</span>
						<span style="font-size: 12px;margin-left: 5px;font-family: 'Noto Sans SC', sans-serif;">{{ '元' }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button icon="edit-pen" text type="primary" v-auth="'account_accAccounts_edit'"
											 @click="formDialogRef.openDialog(scope.row.id)">编辑</el-button>
						<el-button icon="delete" text type="primary" v-auth="'account_accAccounts_del'" @click="handleDelete([scope.row.id])">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination"> </pagination>
		</div>
		
		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		
		<!-- 导入excel (需要在 upms-biz/resources/file 下维护模板) -->
		<upload-excel
			ref="excelUploadRef"
			title="导入"
			url="/account/accAccounts/import"
			temp-url="/admin/sys-file/local/file/accAccounts.xlsx"
			@refreshDataList="getDataList"
		/>
	</div>
</template>

<script setup lang="ts" name="systemAccounts">
import { BasicTableProps, useTable } from "/@/hooks/table";
import {fetchList, delObjs, getToalAmount} from "/@/api/account/accAccounts";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
const excelUploadRef = ref();
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
})

const stateData = reactive({
	totalAssets: 0,
})

//  table hook
const {
	getDataList,
	currentChangeHandle,
	sizeChangeHandle,
	sortChangeHandle,
	downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields()
	// 清空多选
	selectObjs.value = []
	getDataList()
}

// 导出excel
const exportExcel = () => {
	downBlobFile('/account/accAccounts/export', Object.assign(state.queryForm, { ids: selectObjs }), 'accAccounts.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}
	
	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

const router = useRouter();

// 点击账户名称打开账户详情的统计情况
const openDetail = (accountId: any, accountName: string) => {
	console.log('accountId', accountId, accountName)
	router.push({name: 'router.accountTransactionDetail', params: { accountId: accountId }})
}

onMounted(async () => {
	const { data: totalAmount} = await getToalAmount()
	stateData.totalAssets = totalAmount as any;
	
})
</script>
<style lang="scss" scoped>
* {
	font-family: 'Noto Sans SC', sans-serif;
}
.account_title_wrapper {
	margin-bottom: 15px;
	text-align: center;
	
	.account_amount_text {
		font-size: 18px;
		font-family: 'Noto Sans SC', sans-serif;
		color: #99a1a9;
	}
	
	.account_amount_value {
		font-size: 25px;
		font-weight: bold;
		color: #f56c6c;
		font-style: italic
	}
}
</style>
